from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from course import models,courseser
# Create your views here.


class HomeCate(APIView):
    def get(self,request):
        allcate = models.Category.objects.filter(parent=None,flag=True).order_by('order')
        ser = courseser.CategorySerializer(allcate,many=True)
        data = ser.data
        for i in data:
            i['children'] = i['children'][:3]
        return Response({"msg":"ok",'data':data})

# 首页展示二级分类的接口
class HomeChild(APIView):
    def get(self,request):
        # 获取到一级分类的参数
        cate = request.query_params.get('cate','')
        if not cate:
            return Response({"msg":"参数错误"})
        # 根据一级分类的参数获取得到二级分类
        catelist = models.Category.objects.get(id=cate,flag=True)
        ser = courseser.CategorySerializer(catelist)
        data = ser.data
        return Response({"msg":"ok","data":data})

